package com.macro.mall.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.portal.dao.WxCofigMapper;
import com.macro.mall.portal.domain.WxCofig;
import com.macro.mall.portal.domain.vo.VxVo;
import com.macro.mall.portal.util.ConcurrentHashMapCacheUtils;
import com.macro.mall.portal.util.Test;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;


@RestController
@RequestMapping("/wechat")
@Api(tags = "微信", description = "微信配置")
public class WxController {

    @Autowired
    WxCofigMapper wxCofigMapper;


    @RequestMapping(value = "/js", method = RequestMethod.POST)
    @ApiOperation("获取js参数")
    public CommonResult js(@RequestBody VxVo vxVo) throws Exception {
        boolean access_token = ConcurrentHashMapCacheUtils.check("access_token");
        if (access_token == false) {
            Test.getAccessToken();
        }
        String token = (String) ConcurrentHashMapCacheUtils.getCache("access_token");
        JSONObject jsApiTicket = Test.getJsApiTicket(token);

        String ticket = jsApiTicket.getString("ticket");

        Map<String, String> stringStringMap = Test.makeWXTicket(ticket, vxVo.getUrl());

        return CommonResult.success(stringStringMap);
    }

    @RequestMapping(value = "/getUrl", method = RequestMethod.POST)
    @ApiOperation("获取链接参数")
    public CommonResult getUrl(@RequestBody WxCofig wxCofig) throws Exception {
        QueryWrapper<WxCofig> wrapper = new QueryWrapper<>();
        wrapper.eq("bolt", wxCofig.getBolt());
        WxCofig one = wxCofigMapper.selectOne(wrapper);
        return CommonResult.success(one);
    }
}
